package com.hxzy.controller;

import com.hxzy.pojo.Emp;
import com.hxzy.pojo.Result;
import com.hxzy.service.EmpService;
import com.hxzy.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wzm
 * @create 2023-10-11 14:27
 * project_name:stu_sys
 */
@RestController
@Slf4j
public class LoginController {
    @Autowired
    private EmpService empService;
    //登录的方法
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
    Emp e = empService.login(emp);
   if(e != null){
       //如果登录成功生成jwt
       //要封装在jwt中的数据
       Map<String,Object> map = new HashMap<String,Object>();
       map.put("id", e.getId());
       map.put("username", e.getUsername());
       map.put("name", e.getName());
       //生成jwt
       String jwt = JwtUtils.getJwt(map);
       return Result.success(jwt);//满足if成功并且在result中返回了jwt
   }


    //判断登录以后的值是否成功
    return Result.error("用户名或密码错误");
    }
}
